In this lecture, we are going to do the last major bits in our game, which is the displaying of our
stats and also coding the tools.
So let's start off with the stats display.
So we've got two stats here.
We've got the speed and we've got the level.
We want these to change when these values change basically.
So inside the stats display script over here to be added in we're going to be working in here.
So let's start off by making some variables.
So let's reference the.
They guy is equal to script, dot parent and local speed.
Frame is equal to stats display GUI dot display frame.
Dot speed frame.
Local speed label.
Is equal to.
Speed.
Frame dot.
Text label.
Oh.
In here?
Yeah.
Same thing.
Okay.
And then for the level stuff.
So local level frame is equal to stats display guy and dot display frame.
Dot level frame local level label.
Is equal to level frame.
Dot level.
Dot text label and then Local Experience Bar, which is a bar we're going to be editing.
Really.
Um, experience bar is equal to level frame.
Dot frame.
Dot bar frame.
Bar.
Which.
So this here has be called bar.
This bar here.
Okay, cool.
Now remember, if you've changed the names of stuff when you're creating these guys, it'll be different
when you reference it like this.
Here.
Okay, um, from there, we're going to need to get the player and all their stats and stuff as well.
So local player is equal to game dot players.
Local.
When?
Where?
Could a player wait for Cha because it would be going to be added in via the latest script leader stats.
Then get the speed and get the level speed.
It is that.
It.
And then.
Who is.
Remember we.
They use folder.
So local player values is equal to player weight for child player values.
And then local experience is equal to player values.
Okay.
So we want to be we want we're wanting to be to change things.
When the speed, level or experience values are changed in some way, basically.
So when the speed change with simplest thing to do as a speed, just a value, the bar is a bit more
complicated.
Um, so start off as a look at speed.
So speed changed.
Connect function.
Like this here.
So if speed is changed now, we could put in here.
If it's the value, if property is equal to value and stuff like that.
But it's either going to be the name or the value and we're not going to change the name at all.
So there's going to be I'm going to have to be the value.
So um, we want to change the text of this to display.
Um, well how much speed they have.
So speed label dot text is equal to well you'd think just speed dot value like this here.
This is like issue with this though.
If I play the game quickly um, and show you.
If I can load in.
Still taking a while to load in.
There we go and start walking for you.
Do you see the issue there?
Sometimes for some reason it adds in extremely small or basically like say decimal points here.
0.3301.
Um, I think it's because of how, um.
Then add up 0.32 add up nicely to one.
You can see the issue with that basically.
So instead we're going do a bit of rounding.
Just make sure this doesn't happen.
Um, so in this stats display here, basically, um, there's a math dot round function, how it rounds
it to the nearest whole number, and I want it to one decimal place.
So a bit stuck on what to do here, but there's a handy trick way around this.
We can still use a math dot round, um, function here, but instead of just rounding this number,
we're going to round a number that's ten times this and then divide it by ten afterwards.
This will give us the result of one decimal place afterwards.
Um, cool.
That's it for speed.
Then we'll have a look at the level and experience.
Here.
Okay.
So, um, to start off with, I'm going to write down two variables.
One is called the experience required.
And this is going to be the experience required to reach the second level.
Okay.
As you remember, levelling works by you have a certain amount of experience and once you get the amount,
the required amount of experience, you then go up to the next level.
So um, that's how much was required for level two.
And then also a variable called bar percentage.
We're going to use to work out how much of the bar should be showing.
Okay, so this will occur when the level was changed and when the experience is changed.
So the level change is a bit easier to do.
Dot change connect function.
So if the level has been changed, we need to change two things.
We obviously need to change the level text.
So level label dot text is equal to the two string of our current level dot value.
So nothing too complicated there.
But we also need to change amount of experience required.
As for most games, it's not 100 experience to rank up each level because as you progress, you'll be
getting more XP or more experience.
So we're going to use a bit of a formula which increases the experience required to get to the next
level each time.
Now you can do this however you'd like.
Some people like to make a whole massive table full of all for specific numbers, but I think by just
timing the previous experience by a certain number is the easiest way to do this.
So by writing experience required.
Um is equal to.
Now we're going to we're going to use a math dot round here to fix the issue we saw last time, and
also just to make sure that it's a whole number as we're not going to experience to have decimals,
we're going to take the current experience required and just times it by 1.5 like that.
Fair.
Um.
So that means then that if it's 100, it will it will then be 150 next time, which I think is a good
value to add on each time.
Um.
You can change this based on how difficult and stuff you want it to be.
I've just tried out and think 1.5 is a good value to add on, but feel free to try out some other values
there as well.
Okay.
Not too difficult for that's the difficult bit or the most tricky bit of this really comes with the
experience.
So experience.
Dot changed connect function.
So we need to calculate the amount of the bar that's showing.
Now luckily because of this here the bar.
Um.
Size.
Here is the value we're going to be changing its offset.
Zero is starting at zero exp.
And one would be right at the end.
Actually, we need changes a bit.
The bar frame needs to be a bit larger.
Um.
Like that bar frame.
And then there's a bar in here as well.
Okay, perfect.
Cool.
Um, and then one means right at the end, basically.
So, um, we basically need to find a percentage on how far they are to the next level and then um,
calculate out basically.
So the bar percentage, which is what the main bit we're going to use in order to update this scale
value is equal to.
Well, we need to round this to make to a whole number to make sure um is equal to the the current value
of experience they have divided by the experience required.
Um.
Sorry.
This here.
So this okay, this all goes in brackets because we need to times it by 100.
Times by 100.
Divide by 100 like this here, just to make sure that it's, um.
It's all, hang on, the bracket goes.
Hang on a minute.
I've done this right.
Bracket goes.
Bracket this.
Divide by 100.
Times by 100.
No, we don't need to back it there.
Okay.
So this means that it has a set of decimal points.
Um, two decimal points to be specific.
So we can include the percentages quite nicely using this.
So this will be the percentage in order to get to the next level basically.
Okay.
So if the bar percentage is equal to one or is sorry is um bigger than one, in fact, then we need
to um.
Oh, no.
If it's bigger than one, we need to change the level, don't we?
So the, um.
So I know this is if it's a smaller band here.
Sorry.
So if it's smaller than we need to set the experience bar dot size to equal M2 dot new.
Remember that's the, um, data type used for scale for GIS.
Um, the bar percentage 010.
And that's it.
So, um, not too complicated, but just a bit of maths and stuff involved here.
Um, so the percentage on how far they are to the next level, um, is then is 0 to 1.
So that's what we're going to put in the scale value here because the scale goes from 0 to 1.
Now if it is um anything else.
So it's one or it's greater than one.
We want to we want to change the value of experience and also the level as well.
So the experience dot value.
Is equal to experience dot value minus the experience required for that level.
So if if require 150 experience we're back down to 50.
And then set the level dot value to increase by one.
Like that there.
And that is it, that's all.
But that's display stuff that we need to do.
So let's quickly play the game and try this out.
So all this happens after we display at the top.
See if I've done it all correctly.
So we're walking around and yes, nicely it's displaying our stats like that there.
And we haven't got anything to exploit at the moment.
But if we change the experience to, let's say ten and it's just off the leaderboard, not the leaderboard,
it's covered by this bit here.
But if we change it to be like 20, for example, we can see the bar slowly coming up.
3099 is right at the end and then it gets going to get to 100.
It then takes us to level two quite nicely like that there.
So that's really cool.
And we've got stats display working.
If you want the guy here to be displayed a bit higher up, you want to change the ignore null set property
to be true, which will basically ignore the top bar of your game.
So ignore you null set on so it displays at the top.
Um, cool.
Now, the only other bit we need to do now is to code the tools in order to actually give us, give
us.
Um, what am I trying to say?
Um, give us the speed basically.
So over in the items folder, this will be a local script.
Put it into each one.
Um.
So to add here the local script.
Okay cool.
So we're going to call this speed giver.
And we're going to reference web storage.
Local replicated.
Storage is equal to game get service web page storage.
Okay.
Um, now, because this is done on the client, when we're updating the speed, we need to do it on
the server.
So we need to use a remote event for this so we can add in a remote events.
And we're going to call this um item activated.
Because it's activated on the client and we need to do any whenever we add a value onto the speed,
we need to do it on the server.
So it updates everybody.
So let's reference that local item activated is equal to replicate storage.
Wait for child.
Item activated like that there.
Let's get to the top items.
Model script.
Shop items is equal to require replicate storage dot shop items.
As get the tool.
So local tool is equal to script parents.
Let's get the player.
So local player.
Is equal to game players.
Local player.
What else we need to get?
We need to get leader stats and the speed, aren't we?
So local leader stats is equal to player.
Wait for child.
Starts and then local speed.
Is equal leaderstats dot speed.
Okay.
Um, now, I guess the only issue with, like, laying it out the way I have with lots of different
scripts, you have to repeat these variables a lot of times, but it's a bit more familiar and confident.
You can always copy and paste them, script the script and some of them probably aren't needed.
I just like to use create them.
The ones that I think I might need to use.
Um.
So, um, let's what we need to fetch the item.
So let's set item at the moment to be equal to nil and then find the item.
So for I comma v in pairs be shop items items.
Now if the tool dot name is equal to the um tool dot name of the um shop item here, then we know that
the item is equal to v.
So this is basically just determining which item are we using to give the speed.
This is this is a script we're going to use for all of them.
Okay.
So if a tool is activated which activated basically means that this equipped and if left clicked, connect
this with a function.
And we're going to check if um we'll need Debounce in here because we've got a cooldown.
So um let's put here.
Local bounce.
So you could focus by adding in a cool.
We can make sure that this cooldown works basically.
So if it's cooldowns five seconds, we can only do it once every five seconds.
So if it's not currently in a debounce.
We need to say that Debounce is equal to true.
Um, we need to fire our remote event now.
So item activated colon fire server with the speed and with the item dot speed.
Wait, then the item cooldown.
And debounce with n equal to false.
Cool.
So if that's all done correctly, that will fire over a script.
We need to.
We need to work code in server script service for this.
So let's add one in here.
And what could we call this.
Um purchaser.
Um.
It's called speed up or something like that.
Cinemascope.
This is going to be copy out these two.
Okay.
Um isn't activated.
The On.so events Connect function the player passed over as well is the speed and the item speed.
So the player's speed.
And item speed.
Isom speed.
So speed is leaders that speed and item speed.
Yeah.
Is that the speed that you currently have.
Okay.
So all we need to do here is just say speed is equal to speed plus item speed.
And if, if and if I'm not mistaken that's it for this very, very short script here.
To update.
Um, they to just be up to update their speed basically.
Yeah.
Once that's done, it'll come back to here, it'll wait the item cooldown and then the bounce is set
to false.
So you can then repeat it again.
Let's quickly then play this and try this out.
So we load in, we're walking around quite nicely and stuff.
Let's go over to players and give myself a lot of speed.
Let's say a ten.
Okay.
Let's go over to the shop, buy our lollipop in the shop.
We can then equip this.
And does it give us speed?
Oh it doesn't.
I'm left clicking and it doesn't work.
Okay.
Um hmm.
What's the issue here, then?
Um.
Maybe if I get leaderstats.
Speed value, though, isn't it?
Um.
Speed is.
Yeah.
Speed.
Value is what I'm doing, isn't it?
You've got to put value, item speed, item speed.
That should maybe be it then.
I don't know why it's taken so long.
These last few lectures.
It's taken a long time to load in.
I'm not too sure why.
Um, okay, let's go to speed ten shop purchase.
The still not working.
Left clicking and it doesn't work.
Okay.
Um.
What have I done wrong here?
And.
I'm thinking I'm of using speed value here to instead write Leaderstats.
Dot speed.
Dot value like that?
Perhaps.
Is equal to the item speed.
Now, if this doesn't work, I'm going to put some prints in and just see what bits stuck on and which
bit.
Um, yeah.
Which bit stuck on basically.
Okay.
Still not working.
Okay.
It's a simple script.
Don't know what.
Why?
This isn't working, right.
Um.
Speed giver here.
Tour activated.
If not, debounce Debounce is equal to true.
Do that.
Wait to cooldown.
Debounce equal to false.
Right.
Over in here.
Let's print.
Um.
Event received.
Like that, their leader starts to lose that speed of value.
Value plus item speed.
Let's see if he's not printing one of them.
Maybe.
I'm not too sure.
What I've done, I've probably made a really stupid mistake, I think.
Um.
Right.
It's still not working.
Okay.
Right.
Um, it's not printing anything, actually, which is interesting.
Okay.
If can't find out in a minute, I'm going to just skip ahead in the recording until I've figured this
out.
Um.
I figured out why I'm reading away to myself.
I figured out why this only I've only put this script into the apple.
It needs to be into all of them.
Oh, copy this then, and paste it into all of these.
I'm not going to work.
We don't have a script now.
Um.
Oh, see, this is this is the thing.
When you're coding, you can be an amazing scripter.
Um, but it's a simple mistake like that, which you can take you ages to figure out.
Why.
Why?
What's wrong?
Um, so always by making things clear for yourself and, um, making things as simple as possible kind
of avoids issues like that.
Um.
Of course, now it works perfectly like that.
There.
There we go.
So now it's easier.
The the idea is it's now easier for us to get us to gain this speed.
Um, so that we can then get the next item and then get more speed and so on and so on and so on.
So yeah, that's it then for this lecture, we've managed to figure out my stupid mistake there.
And you can join me in the next lecture where we're going to finish off making this game by just adding
some, some final touches and a bit further explanation about what you can kind of do to add add more
on to this game.
So join me in the next lecture where we discuss that.